package main

import (
	pb "Go-Easy/microservice/helloworld/proto"
	"Go-Easy/pkg/microservice/client"
	"Go-Easy/pkg/microservice/credentials"
	"context"
	"google.golang.org/grpc"
	"log"
	"time"
)

func main() {
	//使用PerRPCCredentials方式进行认证.这一方式可将元数据传给server端，一般用于身份验证等场景
	PerRPCCredential := credentials.PerRPCCredential{Metadata: map[string]string{"Token": "DHNFk"}}
	easyClient := client.NewEasyClient("Hello", grpc.WithPerRPCCredentials(PerRPCCredential))

	//这里做一个测试，可以观察一下:
	//1、在开启限流后,reject和Throttling两种ControlBehavior模式下不同的表现
	//2、使用连接池与不使用之间的性能差异
	for {
		conn, err := easyClient.CreateNewClientConnect()
		if err != nil {
			log.Println("connect fail:", err.Error())
		}

		client := pb.NewHelloServiceClient(conn)
		res, err := client.Hello(context.Background(), &pb.HelloRequest{Message: "hello world"})
		easyClient.ConnectClose(conn)
		if err != nil {
			log.Println("call remote func error:", err.Error())
		} else {
			log.Println(res)
		}
		time.Sleep(50 * time.Millisecond)
	}
}
